/**
 * Copyright by e-shion.net
 */
package net.xfok.ui.animation;

import net.xfok.ui.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ViewFlipper;

/**
 * @author 2010-12-27 Howard
 * 
 */
public class SlideImage extends Activity implements OnClickListener,OnTouchListener{
	private static final String TAG="SlideImage";
	private ViewFlipper vf;
	private float oldTouchValue;
	private int[] images={R.drawable.photo1,R.drawable.photo2,R.drawable.photo3,R.drawable.photo4,R.drawable.photo5,R.drawable.photo6};
//	private int id=0;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slide);
		vf=(ViewFlipper)findViewById(R.id.flipper);
		vf.setOnTouchListener(this);
		vf.setOnClickListener(this);
		setViewFlipper();
	}

	private void setViewFlipper(){
		for(int resId:images){
			ImageView imgView=new ImageView(this);
			imgView.setBackgroundResource(resId);
			vf.addView(imgView);
		}
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {	
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			oldTouchValue = event.getX();
			break;
		}
		case MotionEvent.ACTION_UP: {
			float currentX = event.getX();
			Log.i(TAG, "oldTouchValue="+oldTouchValue);
			if (oldTouchValue < currentX) {
				Log.i(TAG, "==========>right:"+event.getX());
				slideRight();
			}
			if (oldTouchValue > currentX) {
				Log.i(TAG, "==========>left:"+event.getX());
				slideLeft();
			}
			break;
		}
		}
		return false;
	}
	
	private void slideLeft(){		
		vf.setInAnimation(SlideAnimation.inFromRightAnimation());
		vf.setOutAnimation(SlideAnimation.outToLeftAnimation());
		vf.showNext();			
	}
	
	private void slideRight(){
		vf.setInAnimation(SlideAnimation.inFromLeftAnimation());
		vf.setOutAnimation(SlideAnimation.outToRightAnimation());
		vf.showPrevious();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
}
